package com.jzo2o.orders.manager.client;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jzo2o.api.foundations.ServeApi;
import com.jzo2o.api.foundations.dto.response.ServeAggregationResDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * sentinelapi熔断降级保护类
 *
 * @Author katana
 * @Date 2025/7/6 11:26
 */
@Component
@Slf4j
public class FoundationsClient {
    
    
    @Resource
    private ServeApi serveApi;
    
    @SentinelResource(value = "getServeBookDetail", fallback = "findServeDetailByIdFallback", blockHandler = "getServeBookDetailBlockHandler")
    public ServeAggregationResDTO findServeDetailById(Long id) {
        log.info("根据id查询服务信息，id:{}", id);
        // 调用其他微服务方法
        ServeAggregationResDTO byId = serveApi.findById(id);
        return byId;
    }
    
    //执行异常走
    public ServeAggregationResDTO findServeDetailByIdFallback(Long id, Throwable throwable) {
        log.error("非限流、熔断等导致的异常执行的降级方法，id:{},throwable:", id, throwable);
        return null;
    }
    
    //熔断后的降级逻辑
    public ServeAggregationResDTO getAddressBookDetailBlockHandler(Long id, BlockException blockException) {
        log.error("触发限流、熔断时执行的降级方法，id:{},blockException:", id, blockException);
        return null;
    }
}
